﻿//-----------------------------------------------------------------------
// <copyright file="LineStyle.cs" company="LMT">
//     Copyright (c) by La Minh Thuận.
// </copyright>
// <author>thuanla1985@hotmail.com</author>
//-----------------------------------------------------------------------
namespace LMT.Forms
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    /// <summary>
    /// Line style
    /// </summary>
    /// <remarks></remarks>
    public enum LineStyle : int
    {
        Zero = 0,
        One = 1,
        Two = 2,
        Three = 3,
        Four = 4,
        Double = 5,
        Triple = 6,
        Seven = 7,
        Eight = 8,
        Nine = 9,
        Ten = 10
    }

    /// <summary>
    /// 
    /// </summary>
    /// <remarks></remarks>
    public enum DrawStyle : int
    {
        DashLine = 0,
        LineStyle = 1
    }

    /// <summary>
    /// 
    /// </summary>
    public enum DashStyle : int
    {
        None = 0,
        Solid = 1,
        RoundDash = 2,
        RoundDashDot = 3,
        RoundDashDotDot = 4,
        RoundDot = 5,
        TriangleDash = 6,
        TriangleDashDot = 7,
        TriangleDashDotDot = 8,
        TriangleDot = 9
    }

    /// <summary>
    /// 
    /// </summary>
    public partial class DashStyleComboBox : ComboBox
    {
        private DrawStyle _itemStyle = DrawStyle.DashLine;
        private DashStyle _dashStyle = DashStyle.Solid;
        private LineStyle _lineStyle = LineStyle.One;

        #region "===== Constructor ====="

        /// <summary>
        /// 
        /// </summary>
        /// <remarks></remarks>
        public DashStyleComboBox()
        {
            // Add any initialization after the InitializeComponent() call.
            this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
            this.DropDownStyle = ComboBoxStyle.DropDownList;
            this.ItemHeight = 16;
            this.MaxDropDownItems = 12;
        }

        #endregion

        #region "===== Properties ====="

        /// <summary>
        /// get or set draw style of combobox
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        public DrawStyle DrawStyle
        {
            get { return this._itemStyle; }
            set
            {
                // clear all previous items
                this.Items.Clear();
                if (value == DrawStyle.DashLine)
                {
                    this.Items.Add(DashStyle.Solid);
                    this.Items.Add(DashStyle.RoundDash);
                    this.Items.Add(DashStyle.RoundDashDot);
                    this.Items.Add(DashStyle.RoundDashDotDot);
                    this.Items.Add(DashStyle.RoundDot);
                    this.Items.Add(DashStyle.TriangleDash);
                    this.Items.Add(DashStyle.TriangleDashDot);
                    this.Items.Add(DashStyle.TriangleDashDotDot);
                    this.Items.Add(DashStyle.TriangleDot);
                }
                else
                {
                    foreach (LineStyle item in Enum.GetValues(typeof(LineStyle)))
                    {
                        this.Items.Add(item);
                    }
                }

                this._itemStyle = value;
            }
        }

        /// <summary>
        /// get or set value of dash style
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [Browsable(false)]
        public DashStyle DashStyle
        {
            get { return this._dashStyle; }
            set
            {
                this._dashStyle = value;

                if (this.Items.Count > 0) this.SelectedIndex = (int)_dashStyle - 1;
            }
        }

        /// <summary>
        /// get or set value of line style
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [Browsable(false)]
        public LineStyle LineStyle
        {
            get { return this._lineStyle; }
            set
            {
                this._lineStyle = value;

                if (this.Items.Count > 0) this.SelectedIndex = (int)_lineStyle - 1;
            }
        }

        #endregion

        #region "===== Overrides Methods ====="

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        /// <remarks></remarks>
        protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
        {
            base.OnDrawItem(e);

            // no selected item to draw
            if (e.Index < 0) return;


            if ((e.State & DrawItemState.Selected) > DrawItemState.None)
            {
                using (SolidBrush selectBrush = new SolidBrush(Color.FromArgb(244, 169, 65)))
                {
                    using (Pen selectPen = new Pen(Color.FromArgb(242, 149, 54), 1))
                    {
                        e.Graphics.FillRectangle(selectBrush, e.Bounds);
                        e.Graphics.DrawLine(selectPen, e.Bounds.X, e.Bounds.Y, e.Bounds.Right, e.Bounds.Bottom - 1);
                    }
                }
            }
            else
            {
                e.Graphics.FillRectangle(Brushes.White, e.Bounds);
            }

            if (this._itemStyle == DrawStyle.DashLine)
            {
                this.DrawDashLine(e);
            }
            else
            {
                this.DrawLineStyle(e);

            }
        }

        /// <summary>
        /// draw line style
        /// </summary>
        /// <param name="e"></param>
        /// <remarks></remarks>
        private void DrawLineStyle(System.Windows.Forms.DrawItemEventArgs e)
        {
            LineStyle lineStyle = default(LineStyle);
            int centerBound = e.Bounds.Bottom - e.Bounds.Height / 2;

            if ((this.Items[e.Index]) is LineStyle)
            {
                lineStyle = (LineStyle)this.Items[e.Index];

                using (Pen dashPen = new Pen(Color.Black))
                {
                    dashPen.Width = (float)lineStyle;
                    //e.Graphics.DrawString(lineStyle & "", Me.Font, Brushes.Black, e.Bounds.Left + 2, e.Bounds.Top + 2)
                    //e.Graphics.DrawLine(dashPen, e.Bounds.Left + 15, centerBound, e.Bounds.Right - 5, centerBound)
                    
                    e.Graphics.DrawString(lineStyle + "", this.Font, Brushes.Black, e.Bounds.Left + 2, e.Bounds.Top + 2);

                    e.Graphics.DrawLine(dashPen, e.Bounds.Left + 20, centerBound, e.Bounds.Right - 5, centerBound);

                }
            }
        }

        /// <summary>
        /// draw dash stype of line
        /// </summary>
        /// <param name="e"></param>
        /// <remarks></remarks>
        private void DrawDashLine(System.Windows.Forms.DrawItemEventArgs e)
        {
            DashStyle dashLine = default(DashStyle);
            int centerBound = e.Bounds.Bottom - e.Bounds.Height / 2;

            if ((this.Items[e.Index]) is DashStyle)
            {
                dashLine = (DashStyle)this.Items[e.Index];

                using (Pen dashPen = new Pen(Color.Black, 5))
                {

                    switch (dashLine)
                    {
                        case DashStyle.Solid:
                            dashPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                            dashPen.DashCap = System.Drawing.Drawing2D.DashCap.Flat;
                            break;
                        case DashStyle.RoundDash:
                            dashPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                            dashPen.DashCap = System.Drawing.Drawing2D.DashCap.Round;
                            break;
                        case DashStyle.RoundDashDot:
                            dashPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
                            dashPen.DashCap = System.Drawing.Drawing2D.DashCap.Round;
                            break;
                        case DashStyle.RoundDashDotDot:
                            dashPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot;
                            dashPen.DashCap = System.Drawing.Drawing2D.DashCap.Round;
                            break;
                        case DashStyle.RoundDot:
                            dashPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                            dashPen.DashCap = System.Drawing.Drawing2D.DashCap.Round;
                            break;
                        case DashStyle.TriangleDash:
                            dashPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                            dashPen.DashCap = System.Drawing.Drawing2D.DashCap.Triangle;
                            break;
                        case DashStyle.TriangleDashDot:
                            dashPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
                            dashPen.DashCap = System.Drawing.Drawing2D.DashCap.Triangle;
                            break;
                        case DashStyle.TriangleDashDotDot:
                            dashPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot;
                            dashPen.DashCap = System.Drawing.Drawing2D.DashCap.Triangle;
                            break;
                        case DashStyle.TriangleDot:
                            dashPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                            dashPen.DashCap = System.Drawing.Drawing2D.DashCap.Triangle;
                            break;
                    }

                    e.Graphics.DrawLine(dashPen, e.Bounds.Left + 5, centerBound, e.Bounds.Right - 5, centerBound);

                }

            }
        }

        /// <summary>
        /// event change value of style on combobox
        /// </summary>
        /// <param name="e"></param>
        /// <remarks></remarks>
        protected override void OnSelectedIndexChanged(System.EventArgs e)
        {
            base.OnSelectedIndexChanged(e);


            if (this._itemStyle == DrawStyle.DashLine)
            {
                this._dashStyle = (DashStyle)Enum.Parse(typeof(DashStyle), this.Items[this.SelectedIndex].ToString());
            }
            else
            {
                this._lineStyle = (LineStyle)Enum.Parse(typeof(LineStyle), this.Items[this.SelectedIndex].ToString());

            }
        }

        #endregion

    }
}
